package com.huige.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huige.mapper.UserMapper;
import com.huige.pojo.User;
import com.huige.service.UserService;
import com.huige.utils.JwtHelper;
import com.huige.utils.MD5Util;
import com.huige.utils.Result;
import com.huige.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author 92746
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-07-21 23:09:00
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;


    /**
     * 登录业务
     *
     *    1.根据账号，查询用户对象 - loginUser
     *    2.如果账号为空，查询失败账号错误！501
     *    3.对比，密码，失败 返回503！密码错误！
     *    4.根据用户id生成一个token，token  -》result 返回
     *
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {

        //根据账号查询数据
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        User loginUser = userMapper.selectOne(wrapper);

        if (loginUser == null) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //对比密码
        if (!StringUtils.isEmpty(user.getUserPwd())
                && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())){
            //登录成功
            //根据用户id生成 token
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            //将token封装到result返回类中
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            return Result.ok(map);
        }
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
    }

    /**
     * 根据token获取用户信息
     *
     * 1.token是否在有效期
     * 2.根据token解析userId
     * 3.根据用户id查询用户数据
     * 4.去掉密码，封装result返回
     *
     * @param token
     * @return
     */
    @Override
    public Result getUserInfo(String token) {

        //是否过期true过期
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) {
            //失效，未登录看待
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        Long userId = jwtHelper.getUserId(token);

        User user = userMapper.selectById(userId);
        user.setUserPwd("");
        Map<String,Object> map = new HashMap<>();
        map.put("loginUser",user);
        return Result.ok(map);
    }

    /**
     * 检查账号是否可用
     *
     * 1.根据账号进行count查询
     * 2.count==0 可用
     * 3.count>0 不可用
     * @param username 账号
     * @return
     */
    @Override
    public Result checkUserName(String username) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);

        Long count = userMapper.selectCount(wrapper);
        if (count==0) {
            return Result.ok(null);
        }
        return Result.build(null, ResultCodeEnum.USERNAME_USED);
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @Override
    public Result regist(User user) {
       user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
       user.setUsername(user.getUsername());
       user.setNickName(user.getNickName());
       LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
       wrapper.eq(User::getUsername,user.getUsername());
        Long count = userMapper.selectCount(wrapper);
        if (count>0) {
            return Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        int insert = userMapper.insert(user);
        return Result.ok(null);
    }
}




